from LeetCoder import *



class Solution:

    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:
        size = len(queries)
        rc, cr = n, n
        ans = 0
        rs , cs = set(),set()
        for id in range(size - 1, -1, -1):
            t, i, v = queries[id]
            if t == 0:  # 修改行
                if i in rs:
                    continue
                rs.add(i)
                ans += rc * v
                cr -= 1
            else:  # 修改列
                if i in cs:
                    continue
                cs.add(i)
                ans += cr * v
                rc -= 1
        return ans
s = Solution()
print(
    s.matrixSumQueries(
        8, [[0, 6, 30094], [0, 7, 99382], [1, 2, 18599], [1, 3, 49292],
            [1, 0, 81549], [1, 1, 38280], [0, 0, 19405], [0, 4, 30065],
            [1, 4, 60826], [1, 5, 9241], [0, 5, 33729], [0, 1, 41456],
            [0, 2, 62692], [0, 3, 30807], [1, 7, 70613], [1, 6, 9506],
            [0, 5, 39344], [1, 0, 44658], [1, 1, 56485], [1, 2, 48112],
            [0, 6, 43384]]))
2783119